/**
 * 学生上下车列表-详情页面使用
 */

var studentRideDetailUrl = "studentRide/studentRideInfo.do";

$(document).ready(function () {
    $('#student-ride-fm').form('load', studentRideDetailUrl + "?id=" + id);
    setTimeout(function(){
    	commonDataGrid();
    	createMap();
    }, 1000);
});


var stationlistUrl="station/stationListForLine.do?lineId=${id}";  //列表展示url


function showMap(){
	 $.ajax({
		url:stationlistUrl, 
		type:"post",
		success:function(data) {
		    loanMap(data);
		    }
		    });
	
}

function loanMap(data){
	// 百度地图API功能
	var map = new BMap.Map("container");
	map.centerAndZoom("北京市", 15);
	map.enableScrollWheelZoom(true);
	var arr = [];
	
	for (var i = 0; i < data.length; i++){
		arr.push(new BMap.Point(data[i].longitude,data[i].latitude));
	}
		
	var driving = new BMap.DrivingRoute(map,
		{onSearchComplete: function(results){
			if (driving.getStatus() == BMAP_STATUS_SUCCESS){
				var plan = driving.getResults().getPlan(0);
				var num = plan.getNumRoutes();
				for(var j =0;j<num ;j++){
				var pts= plan.getRoute(j).getPath();
				var polyline = new BMap.Polyline(pts);
				polyline.setStrokeColor("green");
				//polyline.setStrokeStyle("dashed");
				map.addOverlay(polyline);
		}
			for (var i = 0; i < data.length; i++){
				var myP = new BMap.Point(data[i].longitude,data[i].latitude)
				var m = new BMap.Marker(myP);
				var lab= new BMap.Label(data[i].stationName,{position:myP});
				map.addOverlay(m);
				map.addOverlay(lab);
			}
		}
		}},{renderOptions:{map: map, autoViewport: true}});
		
		
		for (var i = 0; i < arr.length; i++){
		  driving.search(arr[i],arr[i+1]);
		}
		setTimeout(function(){
		  map.setViewport(arr); 
		},100);//使涉及到的点都出现在视野
}


/**
 * 加载学生上下车地点列表
 * 
 * @returns
 */
function commonDataGrid(){
    
	
	var student_name = $("input[name='student_name']").val();
	//var student_name = $("#student_name").val();
	var on_time = $("input[name='on_time']").val();
	var off_time = $("input[name='off_time']").val();
	
	var onLatAndLon = $("input[name='on_latitude']").val() + ',' + $("input[name='on_longitude']").val();
	var offLatAndLon = $("input[name='off_latitude']").val() + ',' + $("input[name='off_longitude']").val();
	var jsonstr = '[{"studentName":"'+student_name+'","onOrOffTime":"'+on_time+'","latAndLon":"'+onLatAndLon+'"},'
		+ '{"studentName":"'+student_name+'","onOrOffTime":"'+off_time+'","latAndLon":"'+offLatAndLon+'"}]';
	console.log(jsonstr);
	//var jsonstr = '{"total":1,"rows":[{"id":"M000005","name":"检测设备","sortid":3,"valid":"1","handler":"系统管理员"}]}';  
	var data = $.parseJSON(jsonstr);    
	$('#goods_Type_Grid').datagrid('loadData', data); 
	
	$("#ride-list").datagrid({
        loadMsg: '数据加载中....',
        pagination: false,
        width: '100%',
        //url: data,
        data : data,
        nowrap: false,
        striped: true,
        sortName: 'id',
        sortOrder: 'asc',
        remoteSort: false,
        fitColumns:true,//宽度自适应
        frozenColumns:[[
					    {field:'id',checkbox:false,hidden: 'true'}
					 ]],
        columns: [
            [
                {field: 'studentName', title: '刷卡学生',width : 30},
                {field: 'onOrOffTime', title: '刷卡时间',width : 30},
                {field: 'latAndLon', title: '经纬度',width : 100}
            ]
        ]
    });// end datagrid
}


function createMap() {
	var map = new BMap.Map("allmap");
	var point = new BMap.Point($("input[name='on_longitude']").val(),$("input[name='on_latitude']").val());
	map.centerAndZoom(point, 15);
	map.enableScrollWheelZoom(true);
	// 编写自定义函数,创建标注
	function addMarker(point, des){
	  var marker = new BMap.Marker(point);
	  map.addOverlay(marker);
	  marker.setAnimation(BMAP_ANIMATION_BOUNCE);
	  
	  var label = new BMap.Label(des,{offset:new BMap.Size(20,-10)});
	  marker.setLabel(label);
	}
	// 随机向地图添加25个标注
	var onPoint = new BMap.Point($("input[name='on_longitude']").val(),$("input[name='on_latitude']").val());
	var offPoint = new BMap.Point($("input[name='off_longitude']").val(), $("input[name='off_latitude']").val());
	addMarker(onPoint,"上车点");
	addMarker(offPoint,"下车点");
}

function saveMenu() {
	
    $('#line-fm').form('submit', {
        url: 'line/lineUpdate.do',
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            result = eval('(' + result + ')');
            if (result.success=='true') {
            	back('line/index.do');
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result.msg
                });
            }
        }
    });
}

function back(url){
	$.get(url, {}, function(data){
		$("#center").empty();
        $("#center").html(data);
        $.parser.parse($("#center"));
    });
}